﻿using System;
using Assist;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Architecture;
using Autodesk.Revit.DB.ExtensibleStorage;
using YArchitech.Revit;

namespace YJKArch.AreaStatistical.DataManager
{
	public class RoomInfoManager
	{
		public static void WriteExtensibleStorageToRoom(Room room, double coefficient, RoomType roomType, bool isBalcony)
		{
			Guid guid = new Guid("E14B3BF5-EE3C-45CA-8977-9AB68E58C118");
			Schema schema = Schema.Lookup(guid);
			if (schema == null)
			{
				SchemaBuilder schemaBuilder = new SchemaBuilder(guid);
				schemaBuilder.SetSchemaName("RoomExtensibleInfo");
				schemaBuilder.AddSimpleField("Coefficient", typeof(double)).SetUnitType(0);
				schemaBuilder.AddSimpleField("IsBalcony", typeof(bool));
				schemaBuilder.AddSimpleField("RoomType", typeof(int));
				schema = schemaBuilder.Finish();
			}
			Entity entity = room.GetEntity(schema);
			if (entity != null)
			{
				room.DeleteEntity(schema);
			}
			Entity entity2 = new Entity(schema);
			Field field = schema.GetField("Coefficient");
			entity2.Set<double>(field, coefficient, (Autodesk.Revit.DB.DisplayUnitType)6);
			Field field2 = schema.GetField("IsBalcony");
			entity2.Set<bool>(field2, isBalcony);
			Field field3 = schema.GetField("RoomType");
			entity2.Set<int>(field3, (int)roomType);
			room.SetEntity(entity2);
		}

		public static bool ReadExtensibleStorageFromRoom(Room room, ref double coefficient, ref RoomType roomType, ref bool isBalcony)
		{
			bool result;
			try
			{
				Parameter parameter = room.GetParameter("面积系数");
				if ((int)parameter.StorageType == 2)
				{
					coefficient = AssistFunc.feetToMM(room.GetParameter("面积系数").AsDouble());
				}
				else if ((int)parameter.StorageType == 3)
				{
					coefficient = Convert.ToDouble(room.GetParameter("面积系数").AsString());
				}
				string text = room.GetParameter("名称").AsString();
				if (text.IndexOf("厅") != -1 || text.IndexOf("起居室") != -1)
				{
					roomType = RoomType.RT_Hall;
				}
				else if (text.IndexOf("室") != -1 || text.IndexOf("卧") != -1)
				{
					roomType = RoomType.RT_Room;
				}
				else if (text.IndexOf("卫") != -1 || text.IndexOf("厕") != -1)
				{
					roomType = RoomType.RT_Cell;
				}
				if (text.IndexOf("阳台") != -1)
				{
					isBalcony = true;
				}
				else
				{
					isBalcony = false;
				}
				result = true;
			}
			catch
			{
				result = false;
			}
			return result;
		}

		public static bool HaxExtensibleStorageInRoom(Room room)
		{
			return room.GetParameter("面积系数") != null;
		}

		private const string m_GUID = "E14B3BF5-EE3C-45CA-8977-9AB68E58C118";

		private const string m_SchemaName = "RoomExtensibleInfo";

		private const string m_CoefficientFieldName = "Coefficient";

		private const string m_IsBalconyFieldName = "IsBalcony";

		private const string m_TypeFieldName = "RoomType";
	}
}
